package com.geek.config.dynamic;

import com.geek.config.standard.User;

import javax.management.*;

public class DynamicUser extends User implements DynamicMBean {
    @Override
    public Object getAttribute(String attribute)
            throws AttributeNotFoundException, MBeanException, ReflectionException {
        switch (attribute) {
            case "userName":
                return getUserName();
            case "password":
                return getPassword();
            default:
                throw new AttributeNotFoundException("不支持");
        }
    }

    @Override
    public void setAttribute(Attribute attribute)
            throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
        switch (attribute.getName()) {
            case "userName":
                setUserName((String)attribute.getValue());
                break;
            case "password":
                setPassword((String) attribute.getValue());
                break;
            default:
                throw new AttributeNotFoundException("不支持");
        }

    }

    @Override
    public AttributeList getAttributes(String[] attributes) {
        AttributeList result = new AttributeList();
        for (String attribute : attributes) {
            try {
                result.add(new Attribute(attribute, getAttribute(attribute)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }



    @Override
    public AttributeList setAttributes(AttributeList attributes) {
        for (Object attribute : attributes) {
            if (attribute instanceof Attribute) {
                try {
                    setAttribute((Attribute) attribute);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return attributes;
    }

    @Override
    public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException {
        switch (actionName) {
            case "getInfo2":
                String info = getInfo();
                System.out.println("info = " + info);
                return info;
            default:
                throw new MBeanException(new IllegalArgumentException("不支持"));
        }
    }

    @Override
    public MBeanInfo getMBeanInfo() {
        return new MBeanInfo(
//                String className,
//                String description,
//                MBeanAttributeInfo[] attributes,
//                MBeanConstructorInfo[] constructors,
//                MBeanOperationInfo[] operations,
//                MBeanNotificationInfo[] notifications)
                getClass().getName(),
                "no description",
                of(mBeanAttributeInfo("userName", String.class, true, true), mBeanAttributeInfo("password", String.class, true, true)),
                of(),
                of(mBeanOperationInfo("getInfo2", String.class)),
                of()

        );
    }

    private MBeanAttributeInfo mBeanAttributeInfo(String name, Class type, boolean isReadable, boolean isWritable) {
        return new MBeanAttributeInfo(name, type.getName(), name, isReadable, isWritable, !(isReadable || isWritable));
    }

    private MBeanOperationInfo mBeanOperationInfo(String methodName, Class returnType) {
        return new MBeanOperationInfo(methodName, methodName, of(), returnType.getName(), MBeanOperationInfo.ACTION);
    }


    private static <T> T[] of(T... values) {
        return values;
    }

}
